<template>
  <div class="mod-sys__login-log">
    <el-form :inline="true" :model="dataForm" @keyup.enter.native="getDataList()">
      <el-form-item>
        <el-input v-model="dataForm.username" placeholder="用户名" clearable></el-input>
      </el-form-item>
      <el-form-item>
        <el-select v-model="dataForm.status" clearable placeholder="请选择状态">
          <el-option label="登录成功" :value="0"></el-option>
          <el-option label="登录失败" :value="1"></el-option>
          <el-option label="账号已锁定" :value="2"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button @click="getDataList()">查询</el-button>
      </el-form-item>
      <el-form-item>
        <el-button type="info" @click="exportHandle()">导出</el-button>
      </el-form-item>
    </el-form>
    <el-table
      :data="dataList"
      border
      v-loading="dataListLoading"
      style="width: 100%">
      <el-table-column
        prop="id"
        header-align="center"
        align="center"
        width="80"
        label="ID">
      </el-table-column>
      <el-table-column
        prop="username"
        header-align="center"
        align="center"
        label="用户名">
      </el-table-column>
      <el-table-column
        prop="operation"
        header-align="center"
        align="center"
        label="用户操作">
      </el-table-column>
      <el-table-column
        prop="status"
        header-align="center"
        align="center"
        label="状态">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.status === 0" type="success">登录成功</el-tag>
          <el-tag v-else-if="scope.row.status === 1" type="danger">登录失败</el-tag>
          <el-tag v-else-if="scope.row.status === 2" type="warning">账号已锁定</el-tag>
        </template>
      </el-table-column>
      <el-table-column
        prop="ip"
        header-align="center"
        align="center"
        label="IP地址">
      </el-table-column>
      <el-table-column
        prop="createDate"
        header-align="center"
        align="center"
        width="180"
        label="登录时间">
      </el-table-column>
    </el-table>
    <el-pagination
      @size-change="sizeChangeHandle"
      @current-change="currentChangeHandle"
      :current-page="pageIndex"
      :page-sizes="[10, 20, 50, 100]"
      :page-size="pageSize"
      :total="totalPage"
      layout="total, sizes, prev, pager, next, jumper">
    </el-pagination>
  </div>
</template>

<script>
  import Cookies from 'js-cookie'
  import qs from 'qs'
  export default {
    data () {
      return {
        dataForm: {
          username: '',
          status: null
        },
        dataList: [],
        pageIndex: 1,
        pageSize: 10,
        totalPage: 0,
        dataListLoading: false,
        selectionDataList: []
      }
    },
    activated () {
      this.getDataList()
    },
    methods: {
      // 获取数据列表
      getDataList () {
        this.dataListLoading = true
        this.$http.get(
          `${window.SITE_CONFIG['baseURL']}/sys/loginlog/list`,
          {
            params: {
              'page': this.pageIndex,
              'limit': this.pageSize,
              'username': this.dataForm.username,
              'status': this.dataForm.status
            }
          }
        ).then(({data}) => {
          if (data && data.code === 0) {
            this.dataList = data.data
            this.totalPage = data.count
          } else {
            this.dataList = []
            this.totalPage = 0
          }
          this.dataListLoading = false
        })
      },
      // 每页数
      sizeChangeHandle (val) {
        this.pageSize = val
        this.pageIndex = 1
        this.getDataList()
      },
      // 当前页
      currentChangeHandle (val) {
        this.pageIndex = val
        this.getDataList()
      },
      // 导出
      exportHandle () {
        var params = {
          'token': Cookies.get('token'),
          'username': this.dataForm.username,
          'status': this.dataForm.status
        }
        window.location.href = `${window.SITE_CONFIG['baseURL']}/sys/loginlog/export?${qs.stringify({params})}`
      }
    }
  }
</script>
